package com.springboot.cloud.demos.producer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.cloud.demos.producer.dao.UserCouponMapper;
import com.springboot.cloud.demos.entity.param.UserCouponQueryParam;
import com.springboot.cloud.demos.entity.po.UserCoupon;
import com.springboot.cloud.demos.producer.service.IUserCouponService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@RefreshScope
public class UserCouponServiceImpl extends ServiceImpl<UserCouponMapper, UserCoupon> implements IUserCouponService {

    @Value("${producer.userCoupon:123}")
    private String value;

    @Override
    @CacheEvict(value = "userCoupon",allEntries=true)
    public boolean add(UserCoupon userCoupon) {
        return this.save(userCoupon);
    }

    @Override
    @CacheEvict(value = "userCoupon", key = "#root.targetClass+'-'+#id")
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    @CacheEvict(value = "userCoupon", key = "#root.targetClass+'-'+#userCoupon.id")
    public boolean update(UserCoupon userCoupon) {
        return this.updateById(userCoupon);
    }

    @Override
    @Cacheable(value = "userCoupon", key = "#root.targetClass+'-'+#id")
    public UserCoupon get(String id) {
        log.info("value:{}", value);
        return this.getById(id);
    }

    @Override
    public List<UserCoupon> query(UserCouponQueryParam userCouponQueryParam) {
        QueryWrapper<UserCoupon> queryWrapper = userCouponQueryParam.build();
        if(StringUtils.isNotEmpty(userCouponQueryParam.getUserId())){
            queryWrapper.eq("user_id", userCouponQueryParam.getUserId());
        }
        if(StringUtils.isNotEmpty(userCouponQueryParam.getStatus())){
            queryWrapper.eq("status", userCouponQueryParam.getStatus());
        }
        queryWrapper.orderByDesc("created_time");
        return this.list(queryWrapper);
    }
}
